import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.htmlparser.Parser;
import org.htmlparser.filters.HasAttributeFilter;
import org.htmlparser.filters.TagNameFilter;
import org.htmlparser.tags.LinkTag;
import org.htmlparser.tags.TableColumn;
import org.htmlparser.tags.TableRow;
import org.htmlparser.tags.TableTag;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class PageParser {

	private static Logger logger = LoggerFactory.getLogger(PageParser.class
			.getName());

	//private static final String INPUT_DIR = "/mads/hobby/it/data/metros";
	private static final String INPUT_DIR = "/mads/hobby/it/data/states";

	private StringBuilder sb = null;

	public static void main(String[] args) {
		logger.debug("Starting..");

		BufferedWriter writer = null;
		try {
			writer = new BufferedWriter(new FileWriter(
					"extractedMetroLinks.txt"));

			PageParser client = new PageParser();
			File dir = new File(INPUT_DIR);
			client.parseDir(dir, writer);
			/*
			client.parseFile(new File(dir,
					"httpwwwashiorghomeinspectorsAlabamaBirminghamaspx.html"),
					writer);
					*/

		} catch (IOException e1) {
			logger.error("Error", e1);
		} catch (ParserException e) {
			logger.error("Error", e);
		} finally {
			try {
				writer.close();
			} catch (IOException e) {
				// ignore
			}
		}
	}

	public void parseDir(File dir, BufferedWriter writer)
			throws ParserException, IOException {

		File[] files = dir.listFiles();
		for (int i = 0; i < files.length; i++) {

			// Read into a string
			sb = new StringBuilder();
			if (files[i].getName().endsWith(".html")) {
				parseFile(files[i], writer);
			}
		}
	}

	private char[] cbuf = new char[1024];

	public void parseFile(File file, BufferedWriter writer) throws IOException,
			ParserException {

		logger.debug("Parsing " + file.getAbsolutePath());
		char[] cbuf = new char[1024];

		// Read into a string
		String content = FileUtils.readFileToString(file);
		Parser parser = new Parser();
		parser.setInputHTML(content);
		parser.setEncoding("utf-8");
		NodeList nodeList = parser.parse(null);

		NodeList tableNodes = nodeList.extractAllNodesThatMatch(
				new HasAttributeFilter("id", "citiesDataList"), true);
		//inspectorsDataList
		if (tableNodes.size() == 0) {
			logger.warn(String.format(
					"%s: Found no table element named citiesDataList", file
							.getAbsolutePath()));
			return;
		}
		
		NodeList aList = tableNodes.extractAllNodesThatMatch(
				new TagNameFilter("a"), true);
		for (int i = 0; i < aList.size(); i++) {
			LinkTag anchor = (LinkTag) aList.elementAt(i);
			if (anchor != null) {
				String path = anchor.extractLink();
				writer.write(String.format("http://www.ashi.org%s", path));
				writer.newLine();
			}
		}
		
		/*
		NodeList tdList = tableNodes.extractAllNodesThatMatch(
				new TagNameFilter("td"), true);
		for (int i = 0; i < tdList.size(); i++) {
			TableColumn td = (TableColumn) tdList.elementAt(i);
			NodeList anchorList = td.getChildren().extractAllNodesThatMatch(
					new TagNameFilter("a"));
			LinkTag anchor = (LinkTag) anchorList.elementAt(0);
			if (anchor != null) {
				String path = anchor.extractLink();
				writer.write(String.format("http://www.ashi.org%s", path));
				writer.newLine();
			}
		}
		*/
		
		/*
		 * TableTag tableTag = (TableTag) tableNodes.elementAt(0); NodeList kids
		 * = tableTag.getChildren(); TableRow[] rows = tableTag.getRows(); for
		 * (int j = 0; j < rows.length; j++) { TableColumn[] cols =
		 * rows[j].getColumns(); for (int k = 0; k < cols.length; k++) { if
		 * (cols[k] != null && cols[k].getChildren() != null) { NodeList
		 * anchorList = cols[k].getChildren() .extractAllNodesThatMatch(new
		 * TagNameFilter("a")); LinkTag anchor = (LinkTag)
		 * anchorList.elementAt(0); if (anchor != null) { String path =
		 * anchor.extractLink();
		 * writer.write(String.format("http://www.ashi.org%s", path));
		 * writer.newLine(); } } } }
		 */
	}

	public PageParser() {
	}

}
